package com.pjedu.nbmallshop.service.imp;

import com.pjedu.nbmallcommon.common.ServiceResultEnum;
import com.pjedu.nbmallcommon.utils.PageResult;
import com.pjedu.nbmallshop.entity.MallCarousel;
import com.pjedu.nbmallshop.mapper.CarouselMapper;
import com.pjedu.nbmallshop.mapper.MallCarouselMapper;
import com.pjedu.nbmallshop.service.MallCarouselService;
import com.pjedu.nbmallshop.vo.MallIndexCarouselVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

// @Service
public class MallCarouselMapperServiceImp implements MallCarouselService {

    @Resource(name = "mallCarouselMapper")
    private MallCarouselMapper carouselMapper;

    //拿到当前页面数据
    @Override
    public PageResult getCarouselPage(HashMap pageUtil) {
        Example example = new Example(MallCarousel.class);
        //因为Example对象可以确定排序规则,所以开始判断参数然后拼凑出排序规则出来
        //如果sidx不为空串的话,就意味着你在表头点击了具体字段的排序
        if (pageUtil.get("sidx")!=null && pageUtil.get("sidx")!=""){
            //然后根据排序的类型-升序还是降序
            if ("asc".equals(pageUtil.get("order").toString())){
                //进行拼接
                example.orderBy(pageUtil.get("sidx").toString()).asc();
            }else {
                example.orderBy(pageUtil.get("sidx").toString()).desc();
            }
        //当sidx为空串的时候,意味着此时你没有点击表头来进行自定义的排序,此时就是一个默认的排序规则
        }else{
            example.orderBy("carouselRank").desc();
        }
        //通过Example对象获取到Criteria对象,用来设置where条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted",0);
        //先拿到Page的变量值
        int pageNum = Integer.parseInt(pageUtil.get("page").toString());
        //构建一个专门用来进行分页的工具
        RowBounds rowBounds = new RowBounds(5*(pageNum - 1),5);//第一个参数代表从第几条数据开始,第二个参数代表展示几条数据
        //执行查询语句
        List<MallCarousel> mallCarousels = carouselMapper.selectByExampleAndRowBounds(example, rowBounds);
        //查到总数量
        example = new Example(MallCarousel.class);
        criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted",0);
        int total = carouselMapper.selectByExample(example).size();
        //拼凑成一个PageResult对象
        PageResult pageResult = new PageResult(mallCarousels, total, Integer.valueOf(pageUtil.get("limit").toString()), Integer.valueOf(pageUtil.get("page").toString()));
        return pageResult;
    }
    //新增轮播图
    @Override
    public String saveCarousel(MallCarousel carousel) {
        if (carouselMapper.insertSelective(carousel) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }
    //更新轮播图
    @Override
    public String updateCarousel(MallCarousel carousel) {
        MallCarousel temp = carouselMapper.selectByPrimaryKey(carousel.getCarouselId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        temp.setCarouselRank(carousel.getCarouselRank());
        temp.setRedirectUrl(carousel.getRedirectUrl());
        temp.setCarouselUrl(carousel.getCarouselUrl());
        temp.setUpdateTime(new Date());
        if (carouselMapper.updateByPrimaryKeySelective(temp) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }
    //点击修改按钮,根据id查询数据
    @Override
    public MallCarousel getCarouselById(Integer id) {
        return carouselMapper.selectByPrimaryKey(id);
    }
    //批量删除
    @Override
    public Boolean deleteBatch(Integer[] ids) {
        if (ids.length < 1) {
            return false;
        }
        Example example = new Example(MallCarousel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("carouselId", Arrays.asList(ids));
        //删除数据
        return carouselMapper.deleteByExample(example)>0;
    }

    @Override
    public List<MallIndexCarouselVO> getCarouselsForIndex(int number) {
        return null;
    }
}
